import React, { useState, useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import { Button, Select, message } from 'antd';
import ProTable from '@ant-design/pro-table';

import BaseInfoShow from '@/components/BaseInfoShow';
import { getAuth } from '@/utils/utils';
import moment from 'moment';
import { history } from 'umi';

const layout = {
  labelCol: { span: 2 },
  wrapperCol: { span: 4 },
};

const ChildRehabilitation = () => {
  const auth = getAuth();
  const actionRef = useRef();

  const [patientId, setPatientId] = useState();
  const [user, setUser] = useState();

  const onAllInfoChange = (user) => {
    setUser(user);
  };

  const onPatientIdChange = (id) => {
    setPatientId(id);
  };

  const handlePreview = () => {
    if (!user?.patientId) return message.info('请先查看患者信息');
    history.push({
      url: '/print/P002',
      query: {
        patientId: user.patientId,
      },
    });
  };

  const ok = () => {
    if (!user?.patientId) return message.info('请先查看患者信息');

  }

  const columns = [
    {
      title: '档案编号',
      dataIndex: 'no',
      search: false,
    },
    {
      title: '日期范围',
      dataIndex: 'createDocumentTime',
      valueType: 'dateRange',
      fieldProps: {
        value: [moment().subtract(1, 'years'), moment()],
      },
      render: (_, record) => {
        return moment(record.createDocumentTime).format('YYYY-MM-DD');
      },
    },
    {
      title: '档案类型',
      dataIndex: 'weight',
      renderFormItem: (_, { type, defaultRender, ...rest }, form) => {
        return (
          <Select placeholder="请选择档案类型">
            <Select.Option value={1}>儿童康复资料</Select.Option>
          </Select>
        );
      },
    },
    {
      title: '生成时间',
      dataIndex: 'visitingTime',
      search: false,
      render: (_, record) => {
        return moment(record.visitingTime).format('YYYY-MM-DD');
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) =>
        auth?.canEdit && (
          <>
            <Button onClick={() => handlePreview} size="small" type="primary" className="mr8">
              预览
            </Button>

            <Button
              onClick={() => {
                printFile({
                  url: `${location.origin}/print/P002?patientId=${user.patientId}`,
                  // url: `http://training.cr.ts-health.cn/print/P002?patientId=${user.patientId}`,
                  fileName: `${user.caseCodeV}-${user.name}-${guide.scaleName}`,
                });
              }}
              danger
              size="small"
              type="primary"
            >
              下载
            </Button>
          </>
        ),
    },
  ];

  return (
    <PageContainer>
      <BaseInfoShow onPatientIdChange={onPatientIdChange} onAllInfoChange={onAllInfoChange} />
      <ProTable
        style={{ width: '100%', marginTop: 20 }}
        actionRef={actionRef}
        rowKey="id"
        request={(params, sorter, filter) => {
          if (patientId) {
            // return getPhysiqueList({ ...params, body: patientId });
          } else {
            return {
              data: [],
              total: 0,
            };
          }
        }}
        toolBarRender={() => [
          <Button onClick={ok} type="primary" key="primary">
            生成档案
          </Button>,
        ]}
        pagination={false}
        columns={columns}
      />

      {/* <Card style={{ marginTop: 20 }}>
        <Form
          {...layout}
          name="basic"
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
        >
          <Form.Item
            label="日期范围"
            name="username"
          >
            <DatePicker.RangePicker />
          </Form.Item>
          <Form.Item
            label="报告类型"
            name="username"
          >
            <Select />
          </Form.Item>

        </Form>
      </Card> */}
    </PageContainer>
  );
};
export default ChildRehabilitation;
